﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System.Linq;
using System.Reflection;
using ConfigGen.Core.SettingsLoader;
using ConfigGen.Utilities.Extensions.System.Reflection;
using Microsoft.Practices.Unity;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.SettingsLoader
{
    [TestFixture]
    public class XmlSettingsLoaderEndToEndTests
    {        
        [Test]
        public void LoadSettings001_ServerLevelInfo()
        {
            var container = new ConfigGenContainer();
            var loader = container.Resolve<XmlSettingsLoader>();

            using (var testFile = Assembly.GetExecutingAssembly().GetEmbeddedResourceFile("SettingsLoader.SampleFiles.App.Config.Settings.xml", "App.Config.Settings.xml"))
            {
                var args = new[] { testFile.FullName };

                var settingsCollection = loader.LoadSettings(args);

                Assert.IsNotNull(settingsCollection, "settingsCollection should not have been null");
                Assert.IsTrue(settingsCollection.MachineConfigurationSettings.Any(), "settingsCollection should not have been empty");

                var settings = settingsCollection.MachineConfigurationSettings.ToList();
                Assert.AreEqual("DevServer", settings[0].MachineName, "Incorrect MachineName 0");
                Assert.AreEqual("App.Config", settings[0].ConfigFilePath, "Incorrect ConfigFilePath 0");

                Assert.AreEqual("MyWorkstation", settings[1].MachineName, "Incorrect MachineName 1");
                Assert.AreEqual("App.Config", settings[1].ConfigFilePath, "Incorrect ConfigFilePath 1");

                Assert.AreEqual("Default", settings[2].MachineName, "Incorrect MachineName 2");
                Assert.AreEqual("App.Config", settings[2].ConfigFilePath, "Incorrect ConfigFilePath 2");

                Assert.AreEqual("UatServer", settings[3].MachineName, "Incorrect MachineName 3");
                Assert.AreEqual("App.Config", settings[3].ConfigFilePath, "Incorrect ConfigFilePath 3");

                Assert.AreEqual("ProdServer", settings[4].MachineName, "Incorrect MachineName 4");
                Assert.AreEqual("App.Config", settings[4].ConfigFilePath, "Incorrect ConfigFilePath 4");
            }
        }

        [Test]
        public void LoadSettings002_Values()
        {
            var container = new ConfigGenContainer();
            var loader = container.Resolve<XmlSettingsLoader>();

            using (var testFile = Assembly.GetExecutingAssembly().GetEmbeddedResourceFile("SettingsLoader.SampleFiles.App.Config.Settings.xml", "App.Config.Settings.xml"))
            {
                var args = new[] { testFile.FullName };

                var settingsCollection = loader.LoadSettings(args);
                var settingsToCheck = new[] {"Environment", "CustomErrorMode" };

                foreach (var settings in settingsCollection.MachineConfigurationSettings)
                {
                    var machineName = settings.MachineName;

                    foreach (var setting in settingsToCheck)
                    {
                        Assert.IsTrue(settings.Contains(setting), setting + " not found for " + machineName);
                    }

                }
            }
        }
    }
}
